﻿@{
    ViewBag.Title = "Ins";
}
@section scripts
{   
    <link href="~/Scripts/miniui/themes/default/medium-mode.css" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js?v=")@ViewBag.version" type="text/javascript"></script>  
    <script src="@Url.Content("~/Scripts/Bill/Bill.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Bill/scan.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/common/Print.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/zxx.drag.1.0.js")" type="text/javascript"></script>
    <style type="text/css">
        .window-mask {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                filter: alpha(opacity=40);
                opacity: 0.40;
                font-size: 1px;
                overflow: hidden;
                display:none;
                background-color: gray;
            }
    </style>

}
<body>
   <form id="form1" runat="server" method="post" style="height: 100%">
    
<div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
    <table width = "1000px;">
        <tr>
            <td width = "1000px;">
                <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="newBills" plain="true" visible="@ViewBag.save">新增</a>
                <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="saveBill" plain="true" visible="@ViewBag.save">保存</a>
                <a class="mini-button" id="ScanAdd" name="ScanAdd" iconCls="icon-find" onclick="ScanAdd" plain="true" visible="@ViewBag.save">扫描</a>
                <a class="mini-button" id="addrow" name="addrow" iconCls="icon-addnew" onclick="addRow" plain="true" visible="@ViewBag.save">增行</a>
                <a class="mini-button" id="delrow" name="delrow" iconCls="icon-remove" onclick="removeRow" plain="true" visible="@ViewBag.save">删行</a>
                <a class="mini-button" id="modify" name="modify" iconCls="icon-edit" onclick="modify" plain="true" visible="@ViewBag.save">改单</a>
                <a class="mini-button" id="check" name="check" iconCls="icon-ok" onclick="checkBill" plain="true" visible="@ViewBag.save">审核</a>
                <a class="mini-button" id="print" iconCls="icon-print" onclick="printBillMyseft" plain="true" visible="@ViewBag.print">打印</a>
                <a class="mini-button" id="getdata" iconCls="icon-collapse" onclick="getBillData" plain="true">浏览</a>
                <span class="separator"></span>
                <a class="mini-button" id="close" iconCls="icon-close" onclick="CloseWindowBill" plain="true">关闭</a>
            </td>
        </tr>
    </table>                 
</div>
           
    <div id = "DivHeader">
        <table style="width:770px;" cellpadding="0" cellspacing="1">
            <tr>
                <td width ="80px" align="right">门店名称：</td>
                <td width ="190px">    
                    @Html.Partial("~/Views/Modular/LookupStockInfo.cshtml")                   
                </td>
                <td width ="80px" align="right">开单日期：</td>
                <td width ="190px">    
                    <input name="FDate" class="mini-datepicker" style="width:190px;"/>
                </td>
                 <td width ="80px" align="right">单据编号：</td>
                 <td width ="150px">    
                    <input name="FBillNo" class="mini-textbox" required="true"readonly="readonly"style="width:150px;"/>
                 </td>
            </tr>
            <tr>
                <td align="right">内容摘要：</td>
                <td >
                    <input name="FContent" class="mini-textbox"  style="width:198px;"/> 
                </td>
                <td align="right">单据备注：</td>                    
                <td >  
                    <input name="FRemark" class="mini-textbox"  style="width:190px;"/>  
                </td>
                <td align="right">单据状态：</td>
                <td >    
                    <input name="txtCheckFlag" class="mini-textbox" readonly="readonly"style="width:150px;"/></td>
            </tr>
        </table>
    </div>
    <div class="mini-fit">
        <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;" 
                showPager = "false" showPageSize="false" showPageIndex="false" skipReadOnlyCell="true"
                allowCellSelect="true" allowSortColumn = "false" allowCellEdit="true" 
                editNextOnEnterKey="true"  editNextRowCell="false" showSummaryRow="true" oncellbeginedit ="onCellBeginEdit"
                ondrawcell="onDrawCell" oncellcommitedit="onCellCommitEdit" ondrawsummarycell="onDrawSummaryCell"
                onrowdblclick="onRowDblClick" fitColumns="false" oncelleditenter="onCelleditenter">
                <div property="columns">            
                <div type="indexcolumn" width="35" headerAlign="center" >序号</div>
                <div field="mFNumber" name="mFNumber" displayField = "mFNumber" width="120" headerAlign="center">商品编码
                <input property="editor" class="mini-buttonedit" onbuttonclick = "onButtonEdit" onvaluechanged = "onValueChanged" style="width:100%;"/>
                </div>  
                <div field="mFName" name="mFName" width="160" headerAlign="center">商品名称</div>  
                <div field="mFModel" name="mFModel" width="100" headerAlign="center">规格型号</div>
                <div field="mFUnitName" name="mFUnitName" width="40" headerAlign="center">单位</div>
                <div field="mFStockQty" name="mFStockQty" numberFormat="#,0.00" width="90" headerAlign="center" align="right" summaryType="sum">库存数量</div>
                <div field="mFQty" name="mFQty" width="70" headerAlign="center" align="right">数量
                    <input property="editor" class="mini-spinner" minValue="0" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0"  />
                </div>
                <div field="mFPrice" name="mFPrice" width="70" numberFormat="#,0.00" headerAlign="center" align="right">库存单价</div>
                <div field="mFAmount" name="mFAmount" numberFormat="#,0.00" width="90" headerAlign="center" align="right" summaryType="sum">金额</div>
                <div field="mFCode" name="mFCode" width="120" headerAlign="center">商品条码</div>
                <div field="mFRemark" name="mFRemark" headerCls="CellLast" width="150" headerAlign="center" >备注
                    <input property="editor" class="mini-textbox" style="width:100%;"/>
                </div>
            </div>
        </div> 
    </div>

     @Html.Partial("~/Views/Modular/Bill/Footer.cshtml")

    </form>

    <input id="type" name="type" value="@ViewBag.type" type="hidden"/>
    @Html.Partial("~/Views/Modular/Bill/ScanWin.cshtml")

    <script type="text/javascript">
        mini.parse();

        var form = new mini.Form("form1");
        var grid = mini.get("datagrid1");      
        var uID = parent.window.getSystemAppID();
        var uCode = parent.window.getSystemAppCode();
        var uName = parent.window.getSystemAppName();
        var LastField = "mFRemark";
        var sumFields = "mFQty,mFAmount,mFStockQty";

        $(function () {
            var oBox = document.getElementById("scan");
            var oBar = document.getElementById("title");
            if(id==0)
                BillState(0);
            InitScan();
            startDrag(oBar, oBox);
             InitGrid('LossData');
       });

        var id = getQueryString("billid");
        if (id != null) {
            mini.getbyName("KInterID").setValue(id);
        }        
        var datatype = getQueryString("datatype");      

        LoadData();

        function newBills() {
            mini.getbyName("FContent").setValue('');
            mini.getbyName("txtCheckFlag").setValue('');
            mini.getbyName("FRemark").setValue('');
            mini.getbyName("FBillNo").setValue('');
            NewBill();
        }

        function LoadData() {
            var minterid = mini.getbyName("KInterID").value;
            $.ajax({
                url: '../Bill/GetBillData',
                data: { mSystemAppID: uID, mBillID: minterid, mDataType: datatype },
                dataType: 'text',
                async: false,
                success: function (text) {
                    var dataR = mini.decode(text);
                    grid.setData(dataR.datadetail);
                    if (dataR.datamaster.length > 0) {
                        var mdata = dataR.datamaster[0];
                        form.setData(mdata);
                        mini.getbyName("FStockID").setValue(mdata.FStockID);
                        mini.getbyName("FStockID").setText(mdata.FStockNumber);
                        mini.get("ScanAdd").setEnabled(false);
                        if (mdata.FCheckFlag == 1) {
                            BillState(2);
                        }
                        else {
                            BillState(1);
                        }
                        if ((mdata.KInterID == "0") || (mdata.KInterID == null)) {
                            newBills();
                        }
                    }
                    else {
                        newBills();
                    }
                }
            });
        }

        function checkValid() {
            if ((mini.getbyName("FBillerID").getValue() == "") || (mini.getbyName("FBillerID").getValue() == "0")) {
                var newbillerid = parent.window.getSystemAppID();
                mini.getbyName("FBillerID").setValue(newbillerid);
            }

            var MainData = form.getData();
            if (MainData.FStockID.toString() == "") {
                mini.showTips({ content: "<b>保存失败</b> <br/>门店不能空值!", state: "danger", x: "center", y: "top", timeout: 3000 });
                return false;
            }
            return true;
        }

        function saveBill() {
            BillState(1);//先锁定
            mini.get("ScanAdd").setEnabled(false);
            if (checkValid() == false) {
                BillState(4);//先锁定
                mini.get("ScanAdd").setEnabled(true);
                return;
            }
            var MainData = form.getData();
            var cDate = mini.getbyName("FDate").getFormValue();
            var rCnt = grid.getData().length;
            var sBillNo = MainData.FBillNo;

            var mjson = "[{'KInterID':'" + MainData.KInterID + "',";
            mjson = mjson + "'FBillNo':'" + MainData.FBillNo + "',";
            mjson = mjson + "'FSystemAppID':'" + uID + "',";
            mjson = mjson + "'FStockID':'" + MainData.FStockID + "',";
            mjson = mjson + "'FDate':'" + cDate + "',";
            mjson = mjson + "'FDataType':'" + datatype + "',";
            mjson = mjson + "'FContent':'" + MainData.FContent + "',";
            mjson = mjson + "'FRemark':'" + MainData.FRemark + "',";
            var json = "'goods_list':[";
            var datarow = 0;
            for (var i = 0; i < rCnt; i++) {
                var rs = grid.getRow(i);
                if (typeof (rs.mFNumber) == "undefined") continue;
                if (datarow > 0)
                    js = ",{";
                else
                    js = "{"
                js = js + '"mFItemID":' + rs.mFItemID + ",";
                var qty = typeof (rs.mFQty) == "undefined" ? 0 : rs.mFQty;
                js = js + '"mFQty":' + qty + ",";
                var mFStockQty = typeof (rs.mFStockQty) == "undefined" ? 0 : rs.mFStockQty;
                js = js + '"mFStockQty":' + mFStockQty + ",";
                var price = typeof (rs.mFPrice) == "undefined" ? 0 : rs.mFPrice;
                js = js + '"mFPrice":' + price + ",";
                var money = typeof (rs.mFAmount) == "undefined" ? 0 : rs.mFAmount;
                js = js + '"mFAmount":' + money + ",";
                var remark = typeof (rs.mFRemark) == "undefined" ? "" : rs.mFRemark;
                js = js + '"mFRemark":"' + remark + '"';
                js = js + "}"
                json = json + js;
                datarow = datarow + 1;
            }
            if (datarow == 0) {
                mini.showTips({ content: "<b>保存失败</b> <br/>没有要保存的数据!", state: "danger", x: "center", y: "top", timeout: 5000 });
                BillState(4);
                mini.get("ScanAdd").setEnabled(true);
                return false;
            }
            json = json + "]";
            mjson = mjson + json + "}]";
            $.ajax({
                url: '../Bill/SaveBillData_Inv',
                data: { mdatajson: mjson },
                type: "post",
                cache: false,
                success: function (txt) {
                    var ret = mini.decode(txt);
                    if (ret.id != -1) {
                        mini.getbyName("KInterID").setValue(ret.id);
                        mini.getbyName("FBillNo").setValue(ret.FBillNo);
                        mini.showTips({ content: "<b>保存成功</b>", state: "success", x: "center", y: "top", timeout: 3000 });
                        BillState(1);
                        mini.get("ScanAdd").setEnabled(false);
                    }
                    else {
                        mini.alert("异常:" + ret.Status);
                    }
                },
                error: function (xhr) {
                    mini.alert("异常:" + xhr.responseText);
                }
            });
        }
    </script>

</body>